<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@include file="/include/common/formbackhead.jsp"%>
<%!
private class PageAction extends BaseAction{
	ResultSet rs = null;
	
	//准备action的环境变量 每次请次都会执行此方法 可在此方法中初如化一些变量
	protected void prepareActionEnv() throws Exception{
		
	}
	
	protected void EndProcess() throws Exception{
		if(form.ationEq("save,delete")){
			onSearchRetrieveResult();
		}
	}
	
	//加載頁面默認數據
	protected boolean onLoadDefault() throws Exception{
		if(!form.isNullOrEmpty("menusn")){
			rs = utils.executeQuery("pan","select * from pan..menu where sn = '"+form.gpsv("menusn")+"'");
			if(rs.next()){
				form.FromResultSet(rs, false);
				if("0".equalsIgnoreCase(form.gpsv("enab"))){
					ret.setWarningInfo("此單已取消!");
					page.getToolbar().disableAllButtonExcept("newnext,save");
				}
			}else{
				ret.setWarningInfo("此單已刪除,如需要請重新新增!");
				page.getToolbar().disableAllButtonExcept("newnext");
			}
			onSearchRetrieveResult();
		}
		return true ;
	}
	
	// 取出查询結果
	protected void onSearchRetrieveResult() throws Exception{
		if(!form.isNullOrEmpty("menusn")){
			rs = utils.executeQuery("pan","select * from pan..menu where lastsn = '"+form.gpsv("menusn")+"'");
			ret.SetSearchResult(rs);
		}
	}	
	
	//執行儲存
	protected void doSave() throws Exception{
		//1.儲存
		CrudDataEntity crud = form.getCrudDataEntity("pan","pan..menu","sn");
		crud.spsv("sn", form.gpsv("menusn"));
		crud.setSpportSimple(true);
		
		crud.spsv("name", form.gpsv("name"));
		crud.spsv("src", form.gpsv("src"));
		crud.spiv("srt", form.gpiv("srt",1));
		crud.spiv("public", form.gpiv("public",1));
		crud.spsv("open", form.gpsv("open","false"));
		crud.spsv("enab", form.gpsv("enab","1"));
		
		
		if(crud.isInsert()){
			crud.spsv("lastsn", form.gpsv("lastsn"));
			crud.setGetLastKey(true);
			crud.appendCols("prgName,crtDay,crtName,crtIp,updDay,updName,updIp");
		}else{
			crud.appendCols("prgName,updDay,updName,updIp");
		}
		crud.save();
		if(form.isNullOrEmpty("menusn")){
			form.spsv("menusn", crud.gpsv("sn"));
		}
		
		//2.儲存目錄明細
		if(!form.isNullOrEmpty("menusn")){
			BaseDataEntities records = form.getrecords("grid1_records");
			for (int i=0;i<records.size();i++){
				BaseDataEntity record = (BaseDataEntity)records.get(i);
				CrudDataEntity menu = record.getCrudDataEntity("pan","pan..menu","sn");
				
				menu.spsv("name", record.gpsv("name"));
				menu.spsv("src", record.gpsv("src"));
				menu.spiv("srt", record.gpiv("srt",1));
				menu.spsv("open", record.gpsv("open","false"));
				menu.spiv("enab", record.gpiv("enab",1));
				if(menu.isInsert()){
					menu.spsv("lastsn", form.gpsv("menusn"));
				}
				
				if(menu.isNullOrEmpty("name")){
					continue;
				}
				
				menu.setSpportSimple(true);
				menu.setUtils(utils);
				menu.appendSaveCols();
				menu.save();
			};
		}
		ret.setOkInfo("儲存成功!");
	}
	
	protected void doDelete() throws Exception{
		if(form.getActionValue().length() == 0 && !form.isNullOrEmpty("menusn")){
			//1.驗證是否有子目錄
			rs = utils.executeQuery("pan", "select count(*) as cnt from pan..menu where lastsn = '"+form.gpsv("menusn")+"'");
			if(rs.next() && rs.getInt("cnt") > 0){
				ret.setErrorInfo("請先刪除子目錄");
				return ;
			}
			
			utils.executeUpdate("pan", "delete from pan..menu where sn = '"+form.gpsv("menusn")+"'");
		}
		
		StringBuilderEx sql = new StringBuilderEx();
		if("deleteRow".equals(form.getActionValue())){
			String sns[] = form.gpsvs("key");
			int icode = 0; 
			if(sns.length>0){
				sql.Append(" delete a from pan..menu a"
				+" where a.sn in(%1$s) "
				+" and a.sn not in(select lastsn from pan.dbo.menu where lastsn in(%1$s) group by lastsn  having COUNT(*) > 0) ",StringHelper.arrayToString(sns,"'",","));
				icode = utils.executeUpdate("pan",sql.toString());
			}
			if(icode > 0){
				ret.setOkInfo("刪除成功!");
			}else{
				ret.setErrorInfo("刪除失敗!");
			}
		}
	}
}
%>
<%@include file="/include/common/formbackend.jsp"%>
